package com.casic.cloud.service;

import com.casic.cloud.dao.UsersRepository;
import com.casic.cloud.pojo.Users;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @program: rare_earth_cloud
 * @description: 用户访问鉴权
 * @author: Tan_sir
 * @create: 2021-06-09 10:49
 **/

@Service
public class JdbcUserDetailsService implements UserDetailsService {

    @Autowired
    private UsersRepository usersRepository;

    /*根据用户名查询用户所有信息，封装UserDetails对象，框架自动匹配*/
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users byUsersName = usersRepository.findByusername(username);
        User user = null;
        if (byUsersName != null) {
            user = new User(byUsersName.getUsername(), byUsersName.getPassword(), new ArrayList<>());
        }
        return user;
    }
}

